<?php

namespace app\common\model\mysql;

use think\Model;

class Product extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'p_product';

    /**
     * 根据where条件获取商品数据
     * @param array $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProductFind(array $where = [], string $field = "*"){
        $result = $this->where($where)->field($field)->find();
        return $result ? $result->toArray() : [];
    }

    /**
     * 商品数量减 X
     * @param $where
     * @param $field
     * @param int $count
     * @return false
     */
    public function saveDecProductField($where, $field, int $count = 1){
        if(empty($field)){
            return false;
        }
        return $this->where($where)->dec($field,$count)->update();
    }

    /**
     * 商品数量加X
     * @param $where
     * @param $field
     * @param int $count
     * @return false
     */
    public function saveInProductField($where, $field, int $count = 1){
        if(empty($field)){
            return false;
        }
        return $this->where($where)->inc($field,$count)->update();
    }

    /**
     * 查询商品数据列表
     * @param array $where
     * @param string $field
     * @return OrderProductWater[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalProduct(array $where = [], string $field = "*"){
        $order = [
            "sort" => "asc",
            "product_id" => "asc"
        ];
        $result = $this->where($where)->field($field)->order($order)->select();
        return $result->toArray();
    }

    /**
     * 查询商品数据列表（分页）
     * @param $where
     * @param $limit
     * @param $page
     * @param $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalProductList($where,$limit,$page,$field){
        $order = [
            "sort" => "asc",
            "product_id" => "asc"
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result->toArray();
    }
}
